<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class activity_job extends Model
{
    public $table = 'activity_job';
    public $timestamps = false;
    protected $fillable = [];
    public $primaryKey = "id";

    // 未参加活动状态值
    const job_type_start = 1;

    // 活动进行中状态值
    const job_type_run = 2;

    // 活动参与成功 可领取状态值
    const job_type_stop = 3;

    // 卡券未领取
    const job_noget = 1;

    // 卡券已领取
    const job_get = 2;

    /**
     * 查询自己是否有正在运行的任务
     * @param $openid
     * @param $activity_id
     */
    protected function curr_activity($openid, $activity_id, $face)
    {
        try {
            $create_type = false;
            $first = activity_user::where('openid', $openid)->first();
            if (empty($first)) {
                // 创建新的job
                $create_type = true;
            } else {
                // 查询参加的活动是否已经组团成功
                $count = activity_user::where(['job_id'=>$first->job_id])->count();
                if ($count < activity_user::usernumber) {
                    fun_respon(0, '您还有活动在组队中');
                } else {
                    // 如果组团活动没有领取不可 重新开启组团
                    if ($first->status == 0) {
                        fun_respon(0, '您参与的上一个活动卡券还未领取成功');
                    }
                    // 创建新的job
                    $create_type = true;
                }
            }

            DB::beginTransaction();
            if ($create_type === true) {
                $add_data = [
                    'openid' => $openid,
                    'activity_id' => $activity_id
                ];
                $job_id = $this->insertGetId($add_data);
                if (!$job_id) {
                    throw new \Exception("创建任务失败");
                }
                // 添加自己的参与记录
                $user_data = [
                    'job_id' => $job_id,
                    'activity_id' => $activity_id,
                    'openid' => $openid,
                    'face' => $face,
                ];
                $res = activity_user::insert($user_data);
                if (!$res) {
                    DB::commit();
                    fun_respon(0, '添加参与记录失败');
                }
                DB::commit();
                fun_respon(1, '活动参与成功');
            }
            DB::commit();
            fun_respon(0, '创建任务失败！');
        } catch (\Exception $ex) {
            DB::rollBack();
            fun_respon(0, $ex->getMessage());
        }
    }

    protected function createjob($openid, $activity_id, $face)
    {
        try {
            DB::beginTransaction();
            $add_data = [
                'openid' => $openid,
                'activity_id' => $activity_id,
                'face' => $face
            ];
            $job_id = $this->insertGetId($add_data);
            if (!$job_id) {
                throw new \Exception("创建任务失败");
            }
            // 添加自己的参与记录
            $user_data = [
                'job_id' => $job_id,
                'activity_id' => $activity_id,
                'openid' => $openid,
                'face' => $face,
            ];
            $res = activity_user::insert($user_data);
            if (!$res) {
                DB::commit();
                fun_respon(0, '添加参与记录失败');
            }
            DB::commit();
            fun_respon(1, '创建任务成功');
        } catch (\Exception $ex) {
            DB::rollBack();
            fun_respon(0, '组团失败');
        }
    }

    /**
     * 查询是否给该好友助力过
     * @param $up_openid
     * @param $openid
     * @return bool
     */
    protected function help_list($up_openid, $openid)
    {
        $job_info = $this->select('id')->where('openid', $up_openid)->get()->toArray();
        $job_list = array_column($job_info, 'id');
        $openid_list = activity_user::select('openid')->whereIn('job_id', $job_list)->get()->toArray();
        $openid_list = array_column($openid_list, 'openid');
        if (in_array($openid, $openid_list)) {
            return true;
        } else {
            return false;
        }

    }


}
